package org.ko.commons.upload;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;

/**
 * 本地文件存储
 * 
 */
@Component
public class FileRepositoryWebImpl implements FileRepository,ServletContextAware {
	private Logger log = LoggerFactory.getLogger(FileRepositoryWebImpl.class);
	
//	@Value("#{sysConfigs['sys.upload.location']}") 
	private String storeLocation;
	public String storeByExt(String path, String ext, MultipartFile file) throws IOException {
		String filename = UploadUtils.generateFilename(path, ext);
		File dest = new File(wrapRootPath(filename));
		dest = UploadUtils.getUniqueFile(dest);
		store(file, dest);
		return filename;
	}
	public boolean remove(String path) throws IOException {
		File dest = new File(wrapRootPath(path));
		return dest.isFile()?dest.delete():false;
	}
	public String storeByPath(String path, MultipartFile file)
			throws IOException {
		String orgName = file.getOriginalFilename();
		String filename = UploadUtils.generateFilename(path, FilenameUtils.getExtension(orgName),false);
		File dest = new File(wrapRootPath(filename));
		dest = UploadUtils.getUniqueFile(dest);
		store(file, dest);
		return filename;
	}
	public String storeByFilename(String filename, MultipartFile file)
			throws IOException {
		File dest = new File(wrapRootPath(filename));
		store(file, dest);
		return filename;
	}

	public String storeByExt(String path, String ext, File file)
			throws IOException {
		String filename = UploadUtils.generateFilename(path, ext);
		File dest = new File(wrapRootPath(filename));
		dest = UploadUtils.getUniqueFile(dest);
		store(file, dest);
		return filename;
	}

	public String storeByFilename(String filename, File file)
			throws IOException {
		File dest = new File(wrapRootPath(filename));
		store(file, dest);
		return filename;
	}

	private void store(MultipartFile file, File dest) throws IOException {
		try {
			UploadUtils.checkDirAndCreate(dest.getParentFile());
			file.transferTo(dest);
		} catch (IOException e) {
			log.error("Transfer file error when upload file", e);
			throw e;
		}
	}

	private void store(File file, File dest) throws IOException {
		try {
			UploadUtils.checkDirAndCreate(dest.getParentFile());
			FileUtils.copyFile(file, dest);
		} catch (IOException e) {
			log.error("Transfer file error when upload file", e);
			throw e;
		}
	}
	private String wrapRootPath(String filename){
		return wrapWebRootPath(filename);
	}
	@SuppressWarnings("unused")
	private String wrapLocalRootPath(String filename){
		if(null==storeLocation)
			storeLocation="D:/";
		return storeLocation+(filename);
	}
	private String wrapWebRootPath(String filename){
		return this.ctx.getRealPath(filename);
	}
	public File retrieve(String name) {
		return new File(wrapRootPath(name));
	}

	private ServletContext ctx;

	public void setServletContext(ServletContext servletContext) {
		this.ctx = servletContext;
	}
}
